package org.yocastlong;

import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.io.Resources;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;

/**
 * @author 朱海龙
 * 2025/5/16 09:29
 */
public class XPathJustRun {

    /**
     * // 构建Document对象
     * DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     * DocumentBuilder builder = factory.newDocumentBuilder();
     * Document        doc     = builder.parse("example.xml");
     * <p>
     * // 创建XPath对象
     * XPath xpath = XPathFactory.newInstance().newXPath();
     * <p>
     * // 编写XPath表达式
     * String expression = "/employees/employee[@id='1']/name";
     * <p>
     * // 执行查询并获取结果
     * String name = (String) xpath.evaluate(expression, doc, XPathConstants.STRING);
     * System.out.println("Employee Name: " + name);
     * <p>
     * // 查询多个节点
     * String   nodesExpression = "/employees/employee";
     * NodeList nodeList        = (NodeList) xpath.evaluate(nodesExpression, doc, XPathConstants.NODESET);
     * System.out.println("Total Employees: " + nodeList.getLength());
     *
     * @param args
     */
    public static void main(String[] args) {

        try {
            DocumentBuilderFactory factory         = DocumentBuilderFactory.newInstance();
            DocumentBuilder        documentBuilder = factory.newDocumentBuilder();
            documentBuilder.setEntityResolver(new XMLMapperEntityResolver());
            InputStream            fileInputStream = Resources.getResourceAsStream("mybatis-config.xml");
            Document               document        = documentBuilder.parse(fileInputStream);


            XPath xPath = XPathFactory.newInstance()
                                      .newXPath();
            String   expression = "//properties/property[@name='database.username']";
            Object   evaluate   = xPath.evaluate(expression, document, XPathConstants.NODESET);
            NodeList nodeList   = (NodeList) evaluate;
            System.out.println(nodeList.getLength());

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node                item       = nodeList.item(i);
                NamedNodeMap        attributes = item.getAttributes();
                final StringBuilder sb         = new StringBuilder();
                sb.append(attributes.getNamedItem("name")
                                    .getNodeValue())
                  .append("\n\t")
                  .append(attributes.getNamedItem("value")
                                    .getNodeValue());
                System.out.println(sb);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
